﻿using NUnit.Framework;
using Crap4Net.Formatters.Crap4JFormatter;
using System;
using System.Collections.Generic;

namespace Crap4Net.UnitTests.FormatterTests.Crap4JFromatterTests
{
    [TestFixture]
    public class Tests
    {

        readonly MethodSignature SomeMethod = new MethodSignature(
            "DontCare", "DontCare", "DontCare", "DontCare", new string[0]
            );

        [Test]
        public void CreateHistogram_NullData_throwsException()
        {
            var target = new HistogramCalculator();

            Assert.Throws<ArgumentNullException>(() => target.CalculateHistogram(null));
        }

        [Test]
        public void CreateHistogram_GivenEmptyData_ReturnsEmptyHistogram()
        {
            var target = new HistogramCalculator();

            var result = target.CalculateHistogram(new List<CombinedDataEntry>());

            CollectionAssert.AreEquivalent(HistogramCalculator.EmptyHistogram, result);
        }

        [Test]
        public void CreateHistogram_OneMethodWithComplexityOne_ReturnsCorrectHistogram()
        {
            var input = new List<CombinedDataEntry>();
            input.Add(new CombinedDataEntry(SomeMethod, 100, 1)); // some method with crap=1

            var target = new HistogramCalculator();

            var result = target.CalculateHistogram(input);

            var expected = HistogramCalculator.EmptyHistogram;
            expected[0] = new HistogramEntry("one", 1);

            CollectionAssert.AreEquivalent(expected, result);
            
        }

        [Test]
        public void CreateHistogram_OneMethodinEachBin_ReturnsCorrectHistogram()
        {
            var input = new List<CombinedDataEntry>();
            input.Add(new CombinedDataEntry(SomeMethod, 100, 1)); // some method with crap=1
            input.Add(new CombinedDataEntry(SomeMethod, 100, 2)); // some method with crap=2
            input.Add(new CombinedDataEntry(SomeMethod, 100, 4)); 
            input.Add(new CombinedDataEntry(SomeMethod, 100, 8)); 
            input.Add(new CombinedDataEntry(SomeMethod, 100, 16)); 
            input.Add(new CombinedDataEntry(SomeMethod, 100, 32)); 
            input.Add(new CombinedDataEntry(SomeMethod, 100, 64)); 
            input.Add(new CombinedDataEntry(SomeMethod, 100, 128)); 
            input.Add(new CombinedDataEntry(SomeMethod, 100, 256)); 

            var target = new HistogramCalculator();

            var result = target.CalculateHistogram(input);

            var expected = new List<HistogramEntry>(){
                new HistogramEntry("one", 1),
                new HistogramEntry("two", 1),
                new HistogramEntry("four", 1),
                new HistogramEntry("eight", 1),
                new HistogramEntry("sixteen", 1),
                new HistogramEntry("thirtytwo", 1),
                new HistogramEntry("sixtyfour", 1),
                new HistogramEntry("one28", 1),
                new HistogramEntry("two56", 1) };

            CollectionAssert.AreEquivalent(expected, result);

        }

        [Test]
        public void CreateHistogram_LargeValues_CountedInLastBin()
        {
            var input = new List<CombinedDataEntry>();
            input.Add(new CombinedDataEntry(SomeMethod, 100, 1000)); // some method with crap=1

            var target = new HistogramCalculator();

            var result = target.CalculateHistogram(input);

            var expected = HistogramCalculator.EmptyHistogram;
            expected[8] = new HistogramEntry("two56", 1);

            CollectionAssert.AreEquivalent(expected, result);

        }


    }
}
